package br.com.educa.modelo.security;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.Version;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.serializable.RooSerializable;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@RooJavaBean
@RooSerializable
@RooToString
@RooJson
@RooJpaActiveRecord(finders = { "findPerfilsByAuthorityEquals" })
public class Perfil implements GrantedAuthority, Serializable {

    @PersistenceContext(unitName = "persistenceUnit")
    transient EntityManager entityManager;

    private static final long serialVersionUID = 1L;

    private String authority;
    
    private String alias;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public static br.com.educa.modelo.security.Perfil fromJsonToPerfil(String json) {
        return new JSONDeserializer<Perfil>().use(null, Perfil.class).deserialize(json);
    }

    public static String toJsonArray(Collection<br.com.educa.modelo.security.Perfil> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static Collection<br.com.educa.modelo.security.Perfil> fromJsonArrayToPerfils(String json) {
        return new JSONDeserializer<List<Perfil>>().use(null, ArrayList.class).use("values", Perfil.class).deserialize(json);
    }

    public static final EntityManager entityManager() {
        EntityManager em = new Perfil().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countPerfils() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Perfil o", Long.class).getSingleResult();
    }

    public static List<br.com.educa.modelo.security.Perfil> findAllPerfils() {
        return entityManager().createQuery("SELECT o FROM Perfil o", Perfil.class).getResultList();
    }

    public static br.com.educa.modelo.security.Perfil findPerfil(Long id) {
        if (id == null) return null;
        return entityManager().find(Perfil.class, id);
    }

    public static List<br.com.educa.modelo.security.Perfil> findPerfilEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Perfil o", Perfil.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Perfil attached = Perfil.findPerfil(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public br.com.educa.modelo.security.Perfil merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Perfil merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public String getAuthority() {
        return this.authority;
    }

    public void setAuthority(String authority) {
        this.authority = authority;
    }

    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return this.version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

	public static TypedQuery<Perfil> findPerfilsByAuthorityEquals(String authority) {
        if (authority == null || authority.length() == 0) throw new IllegalArgumentException("The authority argument is required");
        EntityManager em = Perfil.entityManager();
        TypedQuery<Perfil> q = em.createQuery("SELECT o FROM Perfil AS o WHERE o.authority = :authority", Perfil.class);
        q.setParameter("authority", authority);
        return q;
    }

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Perfil other = (Perfil) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
	

	public String getAlias() {
        return this.alias;
    }

	public void setAlias(String alias) {
        this.alias = alias;
    }
}
